package flex.cc.uac.controller;


import cn.hutool.core.lang.tree.Tree;
import com.google.common.base.Preconditions;
import flex.cc.base.dto.LoginAuthDto;
import flex.cc.core.annotation.LogAnnotation;
import flex.cc.core.support.BaseController;
import flex.cc.uac.dto.menu.UacEditMenuDto;
import flex.cc.uac.entity.UacMenuEntity;
import flex.cc.uac.service.UacMenuService;
import flex.cc.uac.vo.ViewMenuVo;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单表
 *
 * @author zhangzk
 * @email zhangzhikai@sinosoft.com.cn
 * @date 2020-07-11 22:29:37
 */
@RestController
@RequestMapping("/uac/menu")
public class UacMenuController extends BaseController {

    @Resource
    private UacMenuService uacMenuService;

    /**
     * 获取菜单列表数据
     *
     * @return the wrapper
     */
    @PostMapping(value = "/list")
    @ApiOperation(httpMethod = "POST", value = "获取拥有的菜单")
    public Wrapper<List<UacMenuEntity>> queryMenuList() {
        List<UacMenuEntity> menuList = uacMenuService.getMenuList(getLoginAuthDto().getUserId(), null);
        return WrapMapper.ok(menuList);
    }

    /**
     * 菜单管理树列表
     * @return the wrapper
     */
    @PostMapping(value = "/getTree")
    @ApiOperation(httpMethod = "POST", value = "获取菜单树")
    public Wrapper<List<Tree<Long>>> getMenuTreeList() {
        List<Tree<Long>> menuList = uacMenuService.getMenuTree(getLoginAuthDto().getComCode(), null);
        return WrapMapper.ok(menuList);
    }

    /**
     * 编辑菜单.
     *
     */
    @PostMapping(value = "/queryById/{id}")
    @ApiOperation(httpMethod = "POST", value = "编辑菜单")
    public Wrapper<ViewMenuVo> queryMenuVoById(@ApiParam(name = "id", value = "菜单id") @PathVariable Long id) {
        ViewMenuVo menuVo = uacMenuService.getViewVoById(id);
        return WrapMapper.ok(menuVo);
    }

    /**
     * 新增菜单.
     */
    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "新增菜单")
    @LogAnnotation
    public Wrapper saveMenu(@ApiParam(name = "saveMenu", value = "保存菜单") @RequestBody UacEditMenuDto uacMenuAddDto) {
        UacMenuEntity uacMenu = new UacMenuEntity();
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        BeanUtils.copyProperties(uacMenuAddDto, uacMenu);
        uacMenuService.saveUacMenu(uacMenu, loginAuthDto);
        return WrapMapper.ok();
    }

    /**
     * 根据id删除菜单

     */
    @PostMapping(value = "/deleteById/{id}")
    @ApiOperation(httpMethod = "POST", value = "删除菜单")
    @LogAnnotation
    public Wrapper<Boolean> deleteUacMenuById(@ApiParam(name = "id", value = "菜单id") @PathVariable Long id) {
        logger.info(" 根据id删除菜单 id={}", id);
        LoginAuthDto loginAuthDto = getLoginAuthDto();

        Preconditions.checkArgument(id != null, "菜单ID不能为空");

        // 判断此菜单是否有子节点
        boolean hasChild = uacMenuService.checkMenuHasChildMenu(id);
        if (hasChild) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "此菜单含有子菜单, 请先删除子菜单");
        }

        boolean result = uacMenuService.deleteUacMenuById(id, loginAuthDto);
        return super.handleResult(result);
    }

}
